package com.company.netty;

import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;

public class TimeoutHandler extends ChannelDuplexHandler {
    // 读超时处理（IdleStateHandler触发）
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent e = (IdleStateEvent) evt;
            if (e.state() == IdleState.READER_IDLE) {
                // 读超时关闭连接
                ctx.close();
                System.out.println("连接超时关闭: " + ctx.channel().remoteAddress());
            }
        }
    }
}
